import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';

Future<Size> getImageSize({String? imageUrl, String? assetPath, File? imageFile}) async {
  ImageProvider imageProvider;

  if (imageUrl != null) {
    // 网络图片
    imageProvider = NetworkImage(imageUrl);
  } else if (assetPath != null) {
    // 本地资源图片
    imageProvider = AssetImage(assetPath);
  } else if (imageFile != null) {
    // 本地文件图片
    imageProvider = FileImage(imageFile);
  } else {
    throw ArgumentError('You must provide either imageUrl, assetPath, or imageFile.');
  }

  Completer<ui.Image> completer = Completer<ui.Image>();

  imageProvider.resolve(const ImageConfiguration()).addListener(
    ImageStreamListener((ImageInfo info, bool _) {
      completer.complete(info.image);
    }),
  );

  ui.Image image = await completer.future;
  return Size(image.width.toDouble(), image.height.toDouble());
}


